import 'package:flutter/material.dart';
import '../models/investment_data.dart';

class RecommendationList extends StatelessWidget {
  final PersonalizedRecommendation recommendations;

  const RecommendationList({
    super.key,
    required this.recommendations,
  });

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        _buildAssetRecommendations(),
        const SizedBox(height: 24),
        _buildEducationalContent(),
        const SizedBox(height: 24),
        _buildMarketInsights(),
      ],
    );
  }

  Widget _buildAssetRecommendations() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        const Text(
          '推荐资产',
          style: TextStyle(
            fontSize: 18,
            fontWeight: FontWeight.bold,
          ),
        ),
        const SizedBox(height: 12),
        ...recommendations.recommendedAssets.map((asset) => 
          _buildAssetCard(asset)
        ).toList(),
      ],
    );
  }

  Widget _buildAssetCard(RecommendedAsset asset) {
    return Card(
      elevation: 2,
      margin: const EdgeInsets.only(bottom: 12),
      child: Padding(
        padding: const EdgeInsets.all(16),
        child: Row(
          children: [
            Container(
              width: 60,
              height: 60,
              decoration: BoxDecoration(
                color: _getAssetTypeColor(asset.type).withOpacity(0.1),
                borderRadius: BorderRadius.circular(8),
              ),
              child: Icon(
                _getAssetTypeIcon(asset.type),
                color: _getAssetTypeColor(asset.type),
                size: 30,
              ),
            ),
            const SizedBox(width: 16),
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    asset.name,
                    style: const TextStyle(
                      fontSize: 16,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                  const SizedBox(height: 4),
                  Text(
                    asset.description,
                    style: const TextStyle(
                      fontSize: 12,
                      color: Colors.grey,
                    ),
                    maxLines: 2,
                    overflow: TextOverflow.ellipsis,
                  ),
                  const SizedBox(height: 8),
                  Row(
                    children: [
                      _buildConfidenceChip(asset.confidenceScore),
                      const SizedBox(width: 8),
                      _buildRiskLevelChip(asset.riskLevel),
                    ],
                  ),
                ],
              ),
            ),
            Column(
              crossAxisAlignment: CrossAxisAlignment.end,
              children: [
                Text(
                  '${(asset.expectedReturn * 100).toStringAsFixed(1)}%',
                  style: TextStyle(
                    fontSize: 18,
                    fontWeight: FontWeight.bold,
                    color: asset.expectedReturn >= 0 ? Colors.green : Colors.red,
                  ),
                ),
                const Text(
                  '预期收益',
                  style: TextStyle(
                    fontSize: 12,
                    color: Colors.grey,
                  ),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildEducationalContent() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        const Text(
          '学习内容',
          style: TextStyle(
            fontSize: 18,
            fontWeight: FontWeight.bold,
          ),
        ),
        const SizedBox(height: 12),
        SizedBox(
          height: 180,
          child: ListView.builder(
            scrollDirection: Axis.horizontal,
            itemCount: recommendations.educationalContent.length,
            itemBuilder: (context, index) {
              final content = recommendations.educationalContent[index];
              return Container(
                width: 280,
                margin: const EdgeInsets.only(right: 12),
                child: _buildContentCard(content),
              );
            },
          ),
        ),
      ],
    );
  }

  Widget _buildContentCard(ContentItem content) {
    return Card(
      elevation: 2,
      child: InkWell(
        borderRadius: BorderRadius.circular(8),
        onTap: () {
          // 处理内容点击
        },
        child: IntrinsicHeight(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Container(
                height: 80,
                decoration: BoxDecoration(
                  color: _getContentTypeColor(content.type).withOpacity(0.1),
                  borderRadius: const BorderRadius.vertical(top: Radius.circular(8)),
                ),
                child: Center(
                  child: Icon(
                    _getContentTypeIcon(content.type),
                    color: _getContentTypeColor(content.type),
                    size: 32,
                  ),
                ),
              ),
              Padding(
                padding: const EdgeInsets.all(12),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      content.title,
                      style: const TextStyle(
                        fontSize: 14,
                        fontWeight: FontWeight.bold,
                      ),
                      maxLines: 2,
                      overflow: TextOverflow.ellipsis,
                    ),
                    const SizedBox(height: 4),
                    Row(
                      mainAxisSize: MainAxisSize.min,
                      children: [
                        _buildDifficultyChip(content.difficulty),
                        const SizedBox(width: 8),
                        Flexible(
                          child: Text(
                            '${content.estimatedReadTime}分钟',
                            style: const TextStyle(
                              fontSize: 12,
                              color: Colors.grey,
                            ),
                          ),
                        ),
                      ],
                    ),
                  ],
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  Widget _buildMarketInsights() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        const Text(
          '市场洞察',
          style: TextStyle(
            fontSize: 18,
            fontWeight: FontWeight.bold,
          ),
        ),
        const SizedBox(height: 12),
        ...recommendations.marketInsights.map((insight) =>
          _buildInsightCard(insight)
        ).toList(),
      ],
    );
  }

  Widget _buildInsightCard(MarketInsight insight) {
    return Card(
      elevation: 2,
      margin: const EdgeInsets.only(bottom: 12),
      child: Padding(
        padding: const EdgeInsets.all(16),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Expanded(
                  child: Text(
                    insight.title,
                    style: const TextStyle(
                      fontSize: 16,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                ),
                _buildRelevanceChip(insight.relevanceScore),
              ],
            ),
            const SizedBox(height: 8),
            Text(
              insight.summary,
              style: const TextStyle(
                fontSize: 14,
                color: Colors.grey,
              ),
              maxLines: 3,
              overflow: TextOverflow.ellipsis,
            ),
            const SizedBox(height: 8),
            Row(
              children: [
                Icon(
                  _getCategoryIcon(insight.category),
                  size: 16,
                  color: Colors.grey,
                ),
                const SizedBox(width: 4),
                Text(
                  _getCategoryDisplayName(insight.category),
                  style: const TextStyle(
                    fontSize: 12,
                    color: Colors.grey,
                  ),
                ),
                const SizedBox(width: 12),
                Text(
                  _formatDate(insight.publishedDate),
                  style: const TextStyle(
                    fontSize: 12,
                    color: Colors.grey,
                  ),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildConfidenceChip(double score) {
    final color = score >= 0.8 ? Colors.green : 
                 score >= 0.6 ? Colors.orange : Colors.red;
    
    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
      decoration: BoxDecoration(
        color: color.withOpacity(0.1),
        borderRadius: BorderRadius.circular(12),
        border: Border.all(color: color.withOpacity(0.3)),
      ),
      child: Row(
        mainAxisSize: MainAxisSize.min,
        children: [
          Icon(Icons.star, color: color, size: 12),
          const SizedBox(width: 4),
          Text(
            '${(score * 100).toStringAsFixed(0)}%',
            style: TextStyle(
              fontSize: 12,
              color: color,
              fontWeight: FontWeight.bold,
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildRiskLevelChip(RiskLevel riskLevel) {
    final color = riskLevel == RiskLevel.low ? Colors.green :
                 riskLevel == RiskLevel.moderate ? Colors.orange : Colors.red;
    final text = riskLevel == RiskLevel.low ? '低风险' :
                riskLevel == RiskLevel.moderate ? '中风险' : '高风险';
    
    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
      decoration: BoxDecoration(
        color: color.withOpacity(0.1),
        borderRadius: BorderRadius.circular(12),
        border: Border.all(color: color.withOpacity(0.3)),
      ),
      child: Text(
        text,
        style: TextStyle(
          fontSize: 12,
          color: color,
          fontWeight: FontWeight.bold,
        ),
      ),
    );
  }

  Widget _buildDifficultyChip(String difficulty) {
    final color = difficulty == 'beginner' ? Colors.green :
                 difficulty == 'intermediate' ? Colors.orange : Colors.red;
    final text = difficulty == 'beginner' ? '入门' :
                difficulty == 'intermediate' ? '中级' : '高级';
    
    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
      decoration: BoxDecoration(
        color: color.withOpacity(0.1),
        borderRadius: BorderRadius.circular(12),
        border: Border.all(color: color.withOpacity(0.3)),
      ),
      child: Text(
        text,
        style: TextStyle(
          fontSize: 12,
          color: color,
          fontWeight: FontWeight.bold,
        ),
      ),
    );
  }

  Widget _buildRelevanceChip(double score) {
    final color = score >= 0.8 ? Colors.green : 
                 score >= 0.6 ? Colors.orange : Colors.red;
    
    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
      decoration: BoxDecoration(
        color: color.withOpacity(0.1),
        borderRadius: BorderRadius.circular(12),
        border: Border.all(color: color.withOpacity(0.3)),
      ),
      child: Text(
        '相关度 ${(score * 100).toStringAsFixed(0)}%',
        style: TextStyle(
          fontSize: 12,
          color: color,
          fontWeight: FontWeight.bold,
        ),
      ),
    );
  }

  Color _getAssetTypeColor(AssetType type) {
    switch (type) {
      case AssetType.nft:
        return Colors.purple;
      case AssetType.stablecoin:
        return Colors.blue;
      case AssetType.derivative:
        return Colors.orange;
      case AssetType.other:
        return Colors.grey;
    }
  }

  IconData _getAssetTypeIcon(AssetType type) {
    switch (type) {
      case AssetType.nft:
        return Icons.image;
      case AssetType.stablecoin:
        return Icons.account_balance;
      case AssetType.derivative:
        return Icons.show_chart;
      case AssetType.other:
        return Icons.category;
    }
  }

  Color _getContentTypeColor(String type) {
    switch (type) {
      case 'article':
        return Colors.blue;
      case 'video':
        return Colors.red;
      case 'podcast':
        return Colors.purple;
      default:
        return Colors.grey;
    }
  }

  IconData _getContentTypeIcon(String type) {
    switch (type) {
      case 'article':
        return Icons.article;
      case 'video':
        return Icons.videocam;
      case 'podcast':
        return Icons.mic;
      default:
        return Icons.help;
    }
  }

  IconData _getCategoryIcon(String category) {
    switch (category) {
      case 'market_overview':
        return Icons.bar_chart;
      case 'trend_analysis':
        return Icons.trending_up;
      case 'regulatory_news':
        return Icons.gavel;
      case 'technology_update':
        return Icons.computer;
      default:
        return Icons.info;
    }
  }

  String _getCategoryDisplayName(String category) {
    switch (category) {
      case 'market_overview':
        return '市场概览';
      case 'trend_analysis':
        return '趋势分析';
      case 'regulatory_news':
        return '监管动态';
      case 'technology_update':
        return '技术更新';
      default:
        return '其他';
    }
  }

  String _formatDate(DateTime date) {
    final now = DateTime.now();
    final difference = now.difference(date);
    
    if (difference.inHours < 1) {
      return '${difference.inMinutes}分钟前';
    } else if (difference.inDays < 1) {
      return '${difference.inHours}小时前';
    } else {
      return '${difference.inDays}天前';
    }
  }
}